#include<vector>
#include<string>
#include<algorithm>
using namespace std;
class Solution {
public:
    int longestCommonSubsequence(string text1, string text2) {
        vector<int>dp0(text2.size() + 1);
        vector<int>dp1(text2.size() + 1);
        for (int i = 1; i <= text1.size(); ++i) {
            for (int j = 1; j <= text2.size(); ++j) {
                if (text1[i-1] == text2[j-1])
                    dp1[j] = dp0[j - 1] + 1;
                else 
                    dp1[j] = max(dp0[j], dp1[j - 1]);
            }
            swap(dp0, dp1);
        }
        return dp0.back();
    }
};